package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {
        List<Goods> goodsList = goodsService.findGoods();
        model.addAttribute("goodsList", goodsList);
        return "goods";//view's name
    }

    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable("id") Integer id) {
        goodsService.deleteById(id);
        return "redirect:/goods/doGoodsUI";//重定向
    }

    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI() {
        return "goods-add";
    }

    @RequestMapping("doSaveGoods")
    public String doSaveGoods(Goods entity) {
        goodsService.saveGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }

    @RequestMapping("doGoodsUpdateUI")
    public String doGoodsUpdateUI() {
        return "goods-update";
    }

    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.fingById(id);
        model.addAttribute("goods", goods);
        return "goods-update";
    }

    @RequestMapping("doUpdateGoods")
    public String doUpdateGoods(Goods goods) {
        goodsService.updateGoods(goods);
        return "redirect:/goods/doGoodsUI";
    }
}
